#include "UploadPart.as"
#include "RecommendPart.as"
#include "FavoritePart.as"
#include "ExpectPart.as"
#include "CommentPart.as"
#include "ReportPart.as"
#include "ChapterListPart.as"
#include "SpecialInfoPart.as"
#include "AlbumCreationPart.as"
#include "RecordPart.as"
#include "ModifyPart.as"

var smcInstance;
var bookId;
var displayMode:Object;
var dynamicPortletArr:Object;
var commentPageNum:Number;

var recordAlbumDetails:Object;
var chapterListNames:Object;
var recordTaskList:Object;

var selectedAlbumId;
var selectedChapterId;
var selectedTask;

var collabOptValue:Number;
var scopeOptValue:Number;
var selectedScopeGroup;

function init(param, data, slot)
{
	Packages.log(this+".init...id="+Packages.model.Context.address.id);
	
	Packages.model.Context.activeContent=this;

	// Param.init
	smcInstance=mlf.mcmanager.SMCManager.getInstance(slot);
	bookId=Packages.model.Context.address.id;
	displayMode=new Object();
	displayMode["bookInfo"]="normal";
	displayMode["chapterList"]="normal";
	displayMode["commentList"]="normal";

	dynamicPortletArr=new Object();

	// UI.init
	var basic=Packages.system.mlf.GateKeeper.scanElement(this._name, "basic");
	Packages.system.display.JAccordion.initialize(basic);
	var expandPortletArr:Array=new Array();
	expandPortletArr.push(basic["bookInfo"]);
	expandPortletArr.push(basic["chapterList"]);
	expandPortletArr.push(basic["commentList"]);
	var styleObj:Object=new Object();
	styleObj.mainLocation=new Object();
	styleObj.subLocation=new Object();
	styleObj.mainLocation.x=0;
	styleObj.mainLocation.y=40;
	styleObj.mainLocation.width=670;
	styleObj.mainLocation.height=450;
	styleObj.subLocation.bx=0;
	styleObj.subLocation.by=0;
	styleObj.subLocation.dx=345;
	styleObj.subLocation.dy=0;
	styleObj.subLocation.width=325;
	styleObj.subLocation.height=40;
	Packages.system.display.JAccordion.setExpandPortletArr(expandPortletArr, styleObj);

	// Render.init
	Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "bookSpecialTitle"));
	Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "bookPageTitle"));
	Packages.system.display.SceneTransition.rollOut();
	// Event.init
	var evtTargetArr:Array=new Array();
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "BookList_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "BookCreation_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "back_print"));
	Packages.system.events.StateFulButtonEvent.initialize(evtTargetArr);

	invokeInitComplete();
}

function dispose()
{
	Packages.system.mlf.GateKeeper.removeListener("onJAccodionCellEvent", this);
	Packages.system.mlf.GateKeeper.removeListener("onReleaseButtonEvent", this);

	delete recordAlbumDetails;
	delete chapterListNames;
	delete recordTaskList;
}

function invokeInitComplete()
{
	Packages.system.mlf.GateKeeper.addListener("onJAccodionCellEvent", this);
	Packages.system.mlf.GateKeeper.addListener("onReleaseButtonEvent", this);

	loadBookSpecialInfo()
	loadChapterList();
	loadCommentList();
}

function onJAccodionCellEvent(evt:Object)
{
	Packages.log(evt.type+": "+evt.param);

	// neither current mode nor previous mode is expand, then quit
	if (evt.data[evt.param]!="expand"&&displayMode[evt.param]!="expand") return;
	
	if (evt.param=="bookInfo") 
	{
		displayMode.bookInfo=evt.data.bookInfo;
		if (dynamicPortletArr.bookInfo=="bookSpecialInfo")
		{
			loadBookSpecialInfo();
		}
	}
	else if (evt.param=="chapterList") 
	{
		displayMode.chapterList=evt.data.chapterList;
		
		if (dynamicPortletArr.chapterList=="chapterListInfo")
		{
			loadChapterList();
		}
	}
	else if (evt.param=="commentList") 
	{
		displayMode.commentList=evt.data.commentList;

		if (dynamicPortletArr.commentList=="commentListInfo")
		{
			loadCommentList();
		}
	}
}

function onReleaseButtonEvent(evt:Object)
{
	Packages.log(evt.type+": "+evt.param);

	if (evt.param=="bookRecommendSpot")
	{	
		loadBookRecommend(evt.target);
	} 
	else if (evt.param=="recommendConfirmSpot")
	{		
		confirmBookRecommend();
	}
	else if (evt.param=="recommendReturnSpot")
	{
		loadBookSpecialInfo();
	}
	else if (evt.param=="bookFavoriteSpot")
	{
		loadBookFavorite(evt.target);
	}
	else if (evt.param=="favoriteConfirmSpot")
	{
		confirmBookFavorite();
	}
	else if (evt.param=="favoriteReturnSpot")
	{
		loadBookSpecialInfo();
	}
	else if (evt.param=="bookExpectSpot")
	{
		loadBookExpect(evt.target);
	}
	else if (evt.param=="bookModifySpot")
	{
		loadBookModification(evt.target);
	}
	else if (evt.param=="expectConfirmSpot")
	{
		confirmBookExpect();
	}
	else if (evt.param=="expectReturnSpot")
	{
		loadBookSpecialInfo();
	}
	else if (evt.param=="modifyConfirmSpot")
	{
		confirmBookModify();
	}
	else if (evt.param=="modifyReturnSpot")
	{
		loadBookSpecialInfo();
	}
	else if (evt.param=="chapterUploadSpot")
	{
		loadChapterUpload(evt.target);
	}
	else if (evt.param=="uploadFileSpot")
	{
		openFileDialog();
	}
	else if (evt.param=="uploadConfirmSpot")
	{
		confirmChapterUpload();
	}
	else if (evt.param=="uploadReturnSpot")
	{
		loadChapterList();
	}
	else if (evt.param=="chapterReportSpot")
	{
		loadChapterReport(evt.target);
	}
	else if (evt.param=="reportConfirmSpot")
	{
		confirmChapterReport();
	}
	else if (evt.param=="reportReturnSpot")
	{
		loadChapterList();
	}

	else if (evt.param=="commentPostSpot")
	{
		loadCommentCreation();
	}
	else if (evt.param=="commentConfirmSpot")
	{
		confirmBookComment(evt.target);
	}
	else if (evt.param=="commentReturnSpot")
	{
		loadCommentList();
	}
	else if (evt.param=="resultReturnSpot")
	{
		var path=Packages.system.mlf.GateKeeper.scanElement(this._name, "resultReturnPath");
		
		this[path.text].apply(this, null);
	}
	else if (evt.param=="commentMoreSpot")
	{
		expandCommentMore();
	}
	else if (evt.param=="albumCreateSpot")
	{
		loadAlbumCreation(evt.target);
	}
	else if (evt.param=="albumCreateReturnSpot")
	{
		loadChapterList();
	}
	else if (evt.param=="albumCreateConfirmSpot")
	{
		confirmAlbumCreation();
	}
	else if (evt.param=="albumListSpot")
	{
		var cellPortlet=Packages.system.mlf.GateKeeper.scanElement(this._name, "chapterList");
		
		Packages.system.display.JAccordion.invokeReleaseEvent(mlf.mcmanager.PathResolver.drawPath(cellPortlet.header));
	}
	else if (evt.param=="commentListPreSpot")
	{
		previousCommentList();
	}
	else if (evt.param=="commentListNextSpot")
	{
		nextCommentList();
	}
	else if (evt.param=="BookCreation_print")
	{
		Packages.components.SceneAgent.loadScene("content/bookPage/BookCreation");
		// Packages.system.display.SceneTransition.rollIn("content/bookPage/BookCreation");
	}
	else if (evt.param.indexOf("aItem")>-1)
	{
		// selected one Album Item
		selectedAlbumId=evt.param.substr(5);
		// Packages.log("selectedAlbumItemId: "+selectedAlbumItemId, "debug");
		loadChapterList(selectedAlbumId);
	}
	else if (evt.param.indexOf("cItem")>-1)
	{
		selectedChapterId=evt.param.substr(5);
		for (var i=0; i<chapterListNames.count; i++)
		{
			if (chapterListNames.objects[i].chapterId==selectedChapterId)
			{
				Packages.modula.task.Composition.contentId=chapterListNames.objects[i].uuid;
				break;
			}
		}
	}
	else if (evt.param=="chapterRecordSpot")
	{
		// condition
		recordChapter();
	}
	else if (evt.param.indexOf("ollabOptSpot")>-1)
	{
		switchCollabOption(evt.target);
	}
	else if (evt.param.indexOf("ScopeOptSpot")>-1)
	{
		switchScopeOption(evt.target);
	}
	else if (evt.param.indexOf("gItem")>-1)
	{
		selectedScopeGroup=evt.param.substr(5);
	}
	else if (evt.param.indexOf("version")>-1)
	{
		selectVersion(evt.param);
	}
}

function handleResponseResult(rsp:Object)
{
	// Packages.log(this._name+".handleResponseResult");
	// Packages.log("voDataType: "+rsp.vo.instance.type);
	var syncFlag:Boolean=true;
	
	if (rsp.vo.instance.type=="Template")
	{
		var dynamicPortlet=Packages.system.mlf.GateKeeper.scanElement(this._name, rsp.component.data.dynamicPortlet);

		if (dynamicPortlet==null || rsp.component.data.flow)
		{
			Packages.system.mlf.GateKeeper.loadML2byContent(rsp.vo.instance.content);
		}
		else 
		{
			var invokeCallObj:Object;
			if (rsp.component.data.invokeCall!=null)
			{
				syncFlag=false;
				invokeCallObj=new Object();
				invokeCallObj.target=this;
				invokeCallObj.func=rsp.component.data.invokeCall;
				invokeCallObj.param=rsp.component.data.invokeArgs;
			}
			
			Packages.system.mlf.GateKeeper.updateDynamicPortlet(dynamicPortlet, null, rsp.vo.instance.content, invokeCallObj);
		}
	}
	else if (rsp.vo.instance.type=="doWrapper")
	{
		Packages.system.mlf.GateKeeper.setValue(rsp.vo.instance.content, this._name);
	}

	if (rsp.component.data.saved!=null)
	{
		this[rsp.component.data.saved]=rsp.service.result;
	}

	if (rsp.component.data.returnResult)
	{
		rsp.component.data.invokeArgs.push(rsp.service.result);
	}

	if (rsp.component.data.flow)
	{
		flowProcessor.launchEvent();	//
	}

	if (rsp.component.data.invokeCall!=null && syncFlag)
	{
		this[rsp.component.data.invokeCall].apply(this, rsp.component.data.invokeArgs);
	}

	Packages.system.display.Progress.removeInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, rsp.component.data.container));
}
